package br.srv.full.virtual.entities.auth;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import br.srv.full.virtual.bean.base.QueryID;

@Entity
@Table(name = "roles")
@NamedQueries( { @NamedQuery(name = "Role.findRoleForWorker", query = "select r from Role r where r.worker=true") })
public class Role implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8509051955910025268L;

	/*
	 * Role padráo no sistema, deve existir obrigatoriamente para um bom
	 * funcionamento.
	 * 
	 * Representa os usuários que sáo representantes de um cliente, mas náo sáo
	 * necessáriamente revendas de produtos para o mesmo.
	 * 
	 */
	public static final Role CLIENT_WORKER = new Role("ClientWorker");

	public static final QueryID FIND_ROLE_FOR_WORKER = new QueryID(Role.class, "Role.findRoleForWorker");

	@Id
	@Column(name = "rolename", length = 60)
	private String name;

	@Column(name = "description")
	private String description;

//	@OneToMany(fetch = FetchType.EAGER, targetEntity = br.srv.full.virtual.entities.i18n.FriendlyText.class)
//	@JoinColumn(name="")
//	@MapKey(name = "locale")
//	private Map<String, FriendlyText> friendlyName;

	@ManyToMany(fetch = FetchType.EAGER, targetEntity = Login.class, mappedBy = "roles")
	@JoinTable(name = "userroles", joinColumns = @JoinColumn(name = "rolename", referencedColumnName = "rolename"), inverseJoinColumns = @JoinColumn(table = "userlogins", name = "loginid", referencedColumnName = "loginid"))
	private List<Login> logins;

	@Column(nullable = false)
	private Boolean worker = false;

	@Column(nullable = false)
	private Boolean allowing = true;

	/**
	 * @return the worker
	 */
	public Boolean getWorker() {
		return worker;
	}

	/**
	 * @param p_worker
	 *            the worker to set
	 */
	public void setWorker(Boolean p_worker) {
		worker = p_worker;
	}

	/**
	 * @return the allowing
	 */
	public Boolean getAllowing() {
		return allowing;
	}

	/**
	 * @param p_allowing
	 *            the allowing to set
	 */
	public void setAllowing(Boolean p_allowing) {
		allowing = p_allowing;
	}

	public Role() {

	}

	/**
	 * @param p_string
	 */
	public Role(String p_name) {
		setName(p_name);
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Login> getLogins() {
		return logins;
	}

	public void setLogins(List<Login> p_logins) {
		this.logins = p_logins;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object p_obj) {
		if (p_obj instanceof Role) {
			Role l_role = (Role) p_obj;
			return getName().equalsIgnoreCase(l_role.getName());
		} else if (p_obj instanceof String) {
			String l_roleName = (String) p_obj;
			return getName().equalsIgnoreCase(l_roleName);
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return getClass().getSimpleName() + ": " + getName();
	}

	// /**
	// * @return the friendlyName
	// */
	// public Map<String, FriendlyText> getFriendlyName() {
	// return friendlyName;
	// }
	//
	// /**
	// * @param p_nomeAmigavel
	// * the friendlyName to set
	// */
	// public void setFriendlyName(Map<String, FriendlyText> p_nomeAmigavel) {
	// friendlyName = p_nomeAmigavel;
	//	}
}
